﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Diagnostics;
using Telerik.Windows;
using Telerik.Windows.Controls.GridView;
using Telerik.Windows.Controls;
using Accounting.Library;
using csla4Skeleton.UI.SL.NghiepVu.TinhGiaThanh.DM_PhuongPhapTGV;
using Converter;
using System.Resources;
using System.Reflection;

namespace csla4Skeleton.UI.SL.HeThong.HT_ThongTinCongTy
{
    public partial class HT_ThongTinCongTy_ListView : UserControl, Csla.Xaml.ISupportNavigation
    {

        #region ISupportNavigation Members
        List<Control> controls = new List<Control>();
        public bool CreateBookmarkAfterLoadCompleted
        {
            get { return true; }
        }

        public event EventHandler LoadCompleted;

        protected virtual void OnLoadCompleted()
        {
            if (LoadCompleted != null)
                LoadCompleted(this, EventArgs.Empty);
        }

        public void SetParameters(string parameters)
        {
            // no parameters
        }

        public string Title
        {
            get { return "ThongTinCongTy"; }
        }

        #endregion

        #region Load Methods
        public HT_ThongTinCongTy_ListView()
        {
            LocalizationConverter converter = new LocalizationConverter();
            converter.LocalResourceManager = new ResourceManager("csla4Skeleton.UI.SL.HeThong.HT_ThongTinCongTy.Resources", Assembly.GetExecutingAssembly());
            this.Resources.Add("Localization", converter);
            InitializeComponent();
         
            ControlBehavior.ControlBehaviors.Navigation(this.HT_ThongTinCongTy_GeneralInformation_GroupBox, controls);
           

        }

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            if (!System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
            {
                System.Windows.Data.CollectionViewSource HT_ThongTinCongTy_collectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["HT_ThongTinCongTy_ListViewModelSource"];
                HT_ThongTinCongTy_ListViewModel HT_ThongTinCongTy_ListViewModel = new HT_ThongTinCongTy_ListViewModel(BusinessConfiguration.Configuration.GridViewFetchMode);
                HT_ThongTinCongTy_ListViewModel.LoadCompleted += new EventHandler(HT_ThongTinCongTy_ListViewModel_LoadCompleted);
                HT_ThongTinCongTy_collectionViewSource.Source = new List<object> { HT_ThongTinCongTy_ListViewModel };

                System.Windows.Data.CollectionViewSource DM_PhuongPhapTGV_collectionViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["DM_PhuongPhapTGV_ListViewModelSource"];
                DM_PhuongPhapTGV_ListViewModel dm_PhuongPhapTGV_ListViewModel = new DM_PhuongPhapTGV_ListViewModel(BusinessConfiguration.Configuration.ComboboxFetchMode);
                DM_PhuongPhapTGV_collectionViewSource.Source = new List<object> { dm_PhuongPhapTGV_ListViewModel };
            }
        }
        #endregion

        private void ComboBox_KeyUp(object sender, KeyEventArgs e)
        {
            ControlBehavior.ComboBoxBehaviors.ComboBox_KeyUp(sender, e, controls);
        }

        private void Control_KeyUp(object sender, KeyEventArgs e)
        {

            ControlBehavior.ControlBehaviors.Control_KeyUp(sender, e, controls);
        }


        #region View Model Related Methods
        void HT_ThongTinCongTy_ViewModel_Saved(object sender, EventArgs e)
        {
            System.Windows.Data.CollectionViewSource HT_ThongTinCongTy_ListViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["HT_ThongTinCongTy_ListViewModelSource"];
            HT_ThongTinCongTy_ListViewModel vm = ((List<object>)HT_ThongTinCongTy_ListViewSource.Source)[0] as HT_ThongTinCongTy_ListViewModel;
            vm.GetHT_ThongTinCongTy_ListViewModel(BusinessConfiguration.Configuration.GridViewFetchMode);
        }

        void HT_ThongTinCongTy_ListViewModel_LoadCompleted(object sender, EventArgs e)
        {
            System.Windows.Data.CollectionViewSource HT_ThongTinCongTy_ListViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["HT_ThongTinCongTy_ListViewModelSource"];
            HT_ThongTinCongTy_ListViewModel ht_ThongTinCongTy_ListViewModel = ((List<object>)HT_ThongTinCongTy_ListViewSource.Source)[0] as HT_ThongTinCongTy_ListViewModel;

            if (ht_ThongTinCongTy_ListViewModel.Model.getListOfInfo().Count == 0)
            {
                System.Windows.Data.CollectionViewSource HT_ThongTinCongTy_ViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["HT_ThongTinCongTy_ViewModelSource"];
                HT_ThongTinCongTy_ViewModel HT_ThongTinCongTy_ViewModel = new HT_ThongTinCongTy_ViewModel(BusinessConfiguration.Configuration.GridViewFetchMode);
                HT_ThongTinCongTy_ViewModel.Saved += new EventHandler(HT_ThongTinCongTy_ViewModel_Saved);
                HT_ThongTinCongTy_ViewSource.Source = new List<object> { HT_ThongTinCongTy_ViewModel };
            }
            else
            {
                System.Windows.Data.CollectionViewSource HT_ThongTinCongTy_ViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["HT_ThongTinCongTy_ViewModelSource"];
                HT_ThongTinCongTy_ViewModel HT_ThongTinCongTy_ViewModel = new HT_ThongTinCongTy_ViewModel(ht_ThongTinCongTy_ListViewModel.Model.getListOfInfo()[0].Khoa);
                HT_ThongTinCongTy_ViewModel.Saved += new EventHandler(HT_ThongTinCongTy_ViewModel_Saved);
                HT_ThongTinCongTy_ViewSource.Source = new List<object> { HT_ThongTinCongTy_ViewModel };
            }
           
          
        }

      
        #endregion

        private void HT_ThongTinCongTy_SaveClick(object sender, RoutedEventArgs e)
        {
            System.Windows.Data.CollectionViewSource HT_ThongTinCongTy_ViewSource = (System.Windows.Data.CollectionViewSource)this.Resources["HT_ThongTinCongTy_ViewModelSource"];
            HT_ThongTinCongTy_ViewModel vm = ((List<object>)HT_ThongTinCongTy_ViewSource.Source)[0] as HT_ThongTinCongTy_ViewModel;

            if (vm.Model.Khoa == 0)
            {
                MessageBoxResult result = MessageBox.Show("Thông tin chỉ được nhập 1 lần, bạn có chắc là lưu lại không ?", "Lưu thông tin công ty", MessageBoxButton.OKCancel);
                if (result == MessageBoxResult.OK)
                {

                    vm.Save_ThongTinCongTy();
                }
            }
            else
            {
                MessageBoxResult result = MessageBox.Show("Thông tin đã được lưu 1 lần, vì vậy thông tin sẽ không được lưu lần nữa", "Lưu thông tin công ty", MessageBoxButton.OK);
            }
            
        }

    }
}
